# -*- coding: utf-8 -*-
"""
Created on Wed Jun  6 18:56:00 2018

@author: Administrator
"""
import os
import agilepipe
from ..origin import Shell_script


#%%
class Process_method(Shell_script):
    #作用原理：
    #参数说明：
    #参数说明：
    #返回值说明：
    #举例：
    #调用：
    #被调用：
    #bug：
    ############################################################################basic strings
    template_dirname = 'template'
    brainmask_filename = 'BrainMask_05_61x73x61.nii'
    brainmask_filename_gz = 'BrainMask_05_61x73x61.nii.gz'
    
    realign_param_str = r'_realign_param.mat'
    coregister_mat_str = r'_coregister.mat'
    
    #下面这些是为了slicesdir在当前工作目录产生的slicesdir/subj_24-_T1.png拷贝到次函数的输出文件夹。
    slicesdir_str = r'slicesdir'

    #下面是主要用于构成T1像相关文件的路径。
    bet_dirname = r'bet'
    fast_dirname = r'fast'
    fnirt_dirname = r'fnirt'
    #组合出bet及相关命令用到的中间文件的full filepath  下面的是为了构成T1像各种预处理结果的路劲。
    brain_str = r'brain'  #brian前面是没有_因为此文件放在subject/bet/文件夹内。
    brainmask_str = r'_mask'

    csf_str = r'_seg_0'
    gm_str = r'_seg_1'
    wm_str = r'_seg_2'
    warpstruct2mni_str = 'warpstruct2mni'

    def initialize_input_list(self,input_list):
        
        #test if the input_list has four parameters
        ##########################################################################重新整理传入参数
        print('------------------------------------------------------------------------------') #to identify the each cycle
        if len(input_list) != 6:
            raise Exception("incompatible parameter amount ") 
        #caveat:self.previous_filename or self.present_filename is actually filepath,in the future version this should be fixed.
        self.previous_filename = input_list[0]
        self.cmd_flag_dict = input_list[1]
        self.cmd_flag_parameter_list = input_list[2]
        self.present_filename = input_list[3]
        self.process_T1_dir = input_list[4][0]
        self.temp1_dir = input_list[4][1] 
        #self.temp1_dir is used to store the brainmask which is used in class Resample_RS 
        #and Smooth_spatial__SMSP
        self.temp2_dir = input_list[4][2]
        #self.temp2_dir is for temporary useness
        self.modality = input_list[5]


    def concat_inputs(self):
        ###########################################################################重要变量构造
        #拆分present_filename,便于给此处理过程总的中间文件命名。 
        #如；/root/XuanWu_gz/BOLD_DFV-10-2000_DS/subj_240_1_BOLD_DFV-10_DS_RA-2_ST-seqplus_CR-2-0.5_NM-nm-2_warped_brain.nii.gz
        self.medi = self.present_filename.rfind(self.suffix)
        self.length = len(self.present_filename)
        self.front_part = self.present_filename[0:self.medi]
        #front_part=/root/XuanWu_gz/BOLD_DFV-10-2000_DS/subj_240_1_BOLD_DFV-10_DS_RA-2_ST-seqplus_CR-2-0.5_NM-nm-2_warped_brain
        self.end_part = self.present_filename[self.medi:self.length]
        #end_part = .nii.gz
        
        
        self.top_dirpath = os.path.split(self.present_filename)[0]
        #例如top_dir_path = r'/root/XuanWu_gz/BOLD_DFV-10-2000_DS'
        self.toptop_dirpath = os.path.split(self.top_dirpath)[0]
        #eg:toptop_dir_path = r'/root/XuanWu_gz'
        self.present_dirname = os.path.split(self.top_dirpath)[1]
        #for example:r'BOLD_DFV-10-2000_DS' this is used to concatenate the file path for realign_para_file etc.
        
        self.subject = os.path.split(self.present_filename)[1].split(self.modality)[0] 
        print('self.subject:',self.subject)
        #例如subject = r'subj_240_T1',在此函数中主要用于构造T1像路径
        self.subject_stars = self.star + self.subject + self.star
        
        #下面构造用到的T1像即MNI template的路径。需要process_T1_dir   还需要subject 
        #上面三个变量，是以处理步骤命名的文件夹名字，放在subject文件夹下。下面的self.process_T1_dir只适合在处理除T1像意外的其他modality。
        #当处理T1像是应该使用self.self.top_dir
        self.bet_dirpath = self.process_T1_dir + self.os_sep + self.subject + self.os_sep + self.bet_dirname
        self.fast_dirpath = self.process_T1_dir + self.os_sep + self.subject + self.os_sep + self.fast_dirname
        self.fnirt_dirpath = self.process_T1_dir + self.os_sep + self.subject + self.os_sep + self.fnirt_dirname
        #组合出bet后得到的中间文件的full filepath
        self.brain_T1_filepath = self.bet_dirpath + self.os_sep + self.brain_str + self.end_part
        self.brainmask_T1_filepath = self.bet_dirpath + self.os_sep +  self.brain_str + self.brainmask_str + self.end_part
        #组合出fast用到的中间文件的full filepath
        self.csf_filepath = self.fast_dirpath + self.os_sep + self.brain_str + self.csf_str + self.end_part
        self.gm_filepath = self.fast_dirpath + self.os_sep +  self.brain_str + self.gm_str + self.end_part
        self.wm_filepath = self.fast_dirpath + self.os_sep +  self.brain_str + self.wm_str + self.end_part
        
        
        #下面的内容为用到的template
        self.mni152_2mm_brain_filepath = r'${FSLDIR}/data/standard/MNI152_T1_2mm_brain.nii.gz '
        self.mni152_2mm_filepath = r'${FSLDIR}/data/standard/MNI152_T1_2mm.nii.gz '
        self.mni152_2mm_brain_mask_filepath = r'${FSLDIR}/data/standard/MNI152_T1_2mm_brain_mask.nii.gz '
        if not os.path.exists(self.mni152_2mm_brain_filepath) and os.path.exists(self.mni152_2mm_filepath) and os.pathexists( self.mni152_2mm_brain_mask_filepath):
            raise Exception("the MNI152 template is not exist ") 
        self.brainmask_filepath = os.path.split(agilepipe.__file__)[0] + self.os_sep + self.template_dirname + self.os_sep + self.brainmask_filename
        self.brainmask_filepath_gz = os.path.split(agilepipe.__file__)[0] + self.os_sep + self.template_dirname + self.os_sep + self.brainmask_filename_gz
        #下面的内容为用到的从T1到MNI152的transform matrix
        self.warpstruct2mni_filepath = self.fnirt_dirpath + self.os_sep +  self.warpstruct2mni_str + self.end_part
#%%